package net.m2technologies.open_arm;

import org.opengroup.arm40.transaction.*;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
public class OpenArmTransactionSimpleFacade {

    private final ArmTransactionFactory armTransactionFactory;
    private final ArmApplicationDefinition armApplicationDefinition;
    private final ArmApplication armApplication;

    public OpenArmTransactionSimpleFacade(final String fileName, final String applicationName) {
        this(initializeOpenArm(fileName), applicationName);
    }

    public OpenArmTransactionSimpleFacade(final OpenArmConfiguration configuration, final String applicationName) {
        this(initializeOpenArm(configuration), applicationName);
    }

    private OpenArmTransactionSimpleFacade(final ArmTransactionFactory armTransactionFactory,
                                           final String applicationName) {
        this.armTransactionFactory = armTransactionFactory;
        this.armApplicationDefinition = armTransactionFactory.newArmApplicationDefinition(applicationName, null, null);
        this.armApplication = armTransactionFactory.newArmApplication(armApplicationDefinition, null, null, null);
    }

    private static final synchronized ArmTransactionFactory initializeOpenArm(final String fileName) {
        final OpenArmAgent openArmAgent = new OpenArmAgent(fileName);
        return openArmAgent.getTransactionFactory();
    }

    private static final synchronized ArmTransactionFactory initializeOpenArm(final OpenArmConfiguration configuration) {
        final OpenArmAgent openArmAgent = new OpenArmAgent(configuration);
        return openArmAgent.getTransactionFactory();
    }

    public synchronized ArmTransactionDefinition getArmTransactionDefinition(final String transactionName) {
        return armTransactionFactory.newArmTransactionDefinition(
                armApplicationDefinition,
                transactionName,
                null,
                armTransactionFactory.newArmID(null));
    }

    public synchronized ArmTransaction getArmTransactionMonitor(final ArmTransactionDefinition transDef) {
        return armTransactionFactory.newArmTransaction(armApplication, transDef);
    }

    public synchronized ArmTransaction getArmTransactionMonitor(final String transactionName) {
        return getArmTransactionMonitor(getArmTransactionDefinition(transactionName));
    }

}
